package ac.svn.accesscontrol.utils;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

import ac.notification.event.NotificationEvent;
import ac.notification.manager.NotificationRegistrar;
import ac.notification.service.INotificationIdetifier;
import ac.svn.accesscontrol.cache.IconCache;


public class Utility 
{
	//Member variables
	private static Map<String,Image> imageCache = new HashMap<String, Image>();
	public static IconCache iconCache = new IconCache();

	static 
	{
		iconCache.load();
	}

	public static String getGUID()
	{
		return UUID.randomUUID().toString();
	}

	public static Image getImage(String url)
	{
		Image image = imageCache.get(url);
		if(null == image)
		{
			System.out.println("[Utils] Fetching Image: " + url);
			image = Toolkit.getDefaultToolkit().getImage(Utility.class.getClassLoader().getResource(url));
			imageCache.put(url, image);
		}
		return image;
	}

	public static ImageIcon getImageIcon(String url)
	{
		ImageIcon icon = iconCache.get(url);
		if(null == icon)
		{
			System.out.println("[Utils] Fetching Image: " + url);
			Image image = Toolkit.getDefaultToolkit().getImage(Utility.class.getClassLoader().getResource(url));
			icon = new ImageIcon(image);
			iconCache.put(url, icon);
		}
		return icon; 
	}

	public static void addEscapeListener(final JDialog component) {
		ActionListener escListener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				component.setVisible(false);
			}
		};
		component.getRootPane().registerKeyboardAction(escListener,
				KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
				JComponent.WHEN_IN_FOCUSED_WINDOW);

	}

	public static void addEscapeListener(final JFrame component) {
		ActionListener escListener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				component.setVisible(false);
			}
		};
		component.getRootPane().registerKeyboardAction(escListener,
				KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
				JComponent.WHEN_IN_FOCUSED_WINDOW);

	}

	public static void setLocationToMiddleOfScreen(JFrame frame)
	{
		Dimension sizeOfScr = Toolkit.getDefaultToolkit().getScreenSize();
		frame.setLocation(((int)(sizeOfScr.getWidth()/2 - frame.getWidth()/2)),((int)(sizeOfScr.getHeight()/2 - frame.getHeight()/2)));
	}

	public static void setLocationToMiddleOfScreen(JDialog dialog)
	{
		Dimension sizeOfScr = Toolkit.getDefaultToolkit().getScreenSize();
		dialog.setLocation(((int)(sizeOfScr.getWidth()/2 - dialog.getWidth()/2)),((int)(sizeOfScr.getHeight()/2 - dialog.getHeight()/2)));
	}

	public static void sendNotification(String notificationId)
	{
		NotificationEvent event = new NotificationEvent(notificationId);
		NotificationRegistrar.dispatchNotification(event);
	}

	public static void sendNotification(String notificationId,Object value)
	{
		NotificationEvent event = new NotificationEvent(notificationId);
		event.setPropertyValue(value);
		NotificationRegistrar.dispatchNotification(event);
	}

	public static void showSystemNotification(String message)
	{
		NotificationEvent event = new NotificationEvent(INotificationIdetifier.N_SHOW_NOTIFICATION_MESSAGE);
		event.setPropertyValue(message);
		NotificationRegistrar.dispatchNotification(event);
	}

	public static void asynchronousUpdate(final Component component)
	{
		try
		{
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					SwingUtilities.updateComponentTreeUI(component);
				}
			});
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	public static String toHTML(String target,boolean b,boolean i, boolean u)
	{
		String str = "<HTML>";
		if(b)
		{
			str += "<b>";
		}

		if(u)
		{
			str += "<u>";
		}

		if(i)
		{
			str += "<i>";
		}

		str += target;
		str += "</b></i></u></HTML>";
		return str;
	}

	public static void addAnimatedClosing(final JFrame targetWindow)
	{
		if(null != targetWindow)
		{
			targetWindow.addWindowListener(new WindowListener() {

				@Override
				public void windowOpened(WindowEvent arg0) {
					SwingUtilities.invokeLater(new Runnable() {

						@Override
						public void run() {
							targetWindow.setVisible(true);
							targetWindow.setExtendedState(JFrame.NORMAL);
							try{	Thread.sleep(10);} catch (InterruptedException e) {}
							targetWindow.setExtendedState(JFrame.MAXIMIZED_BOTH);							
						}
					});

				}

				@Override
				public void windowIconified(WindowEvent arg0) {
					// TODO Auto-generated method stub

				}

				@Override
				public void windowDeiconified(WindowEvent arg0) {
					// TODO Auto-generated method stub

				}

				@Override
				public void windowDeactivated(WindowEvent arg0) {
					// TODO Auto-generated method stub

				}

				@Override
				public void windowClosing(WindowEvent arg0) {
					SwingUtilities.invokeLater(new Runnable() {
						@Override
						public void run() {
							targetWindow.setExtendedState(JFrame.ICONIFIED);
							try{	Thread.sleep(10);} catch (InterruptedException e) {}
							targetWindow.setVisible(false);

						}
					});
				}

				@Override
				public void windowClosed(WindowEvent arg0) {


				}

				@Override
				public void windowActivated(WindowEvent arg0) {
					// TODO Auto-generated method stub

				}
			});
		}
	}


}
